package com.ggdog.blog.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.Map;

public class JwtUtil {
    private static final String KEY="GGDog";

    /**
     * 生成token
     * @param claims
     * @param expireDate
     * @return
     */
    public static String createToken(Map<String, Object> claims, Date expireDate){
        return JWT.create()
                .withClaim("claims",claims)
                .withExpiresAt(expireDate)
                .sign(Algorithm.HMAC256(KEY));
    }

    public static Map<String,Object> validateToken(String token){
        return JWT.require(Algorithm.HMAC256(KEY))
                .build()
                .verify(token)
                .getClaim("claims")
                .asMap();
    }
}
